$(function () {
    getGradeList();
    allPaikemobanTable();
})


/*获取年级下拉数据*/
function getGradeList() {
    $.DemoPost("/classInfo/getClassListByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":100},
        function (data) {
            if(data.success)
            {
                $.each(data.obj.list,function (i,d) {
                    $("#classList").append(" <option value='"+d.id+"'>"+d.gradeName+""+d.className+"</option>");
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}


function query(params) {
    var param = {
        pageNum : params.offset / params.limit + 1,
        pageSize : params.limit
    };
    return param;
}

function allPaikemobanTable() {
    $("#paike_list").bootstrapTable({
        url:$.ip+"/courseMould/getCourseMouldByUserInfo?token="+sessionStorage.getItem("token"),
        method:'post',
        queryParams:query,
        sidePagination: "server",
        pagination: true,
        striped:true,
        pageNumber:1,
        pageSize: 10,
        pageList: [10, 25, 50, 100],
        dataType:"json",
        contentType:"application/json",
        responseHandler:function (res) {
            return {
                total:res.obj.total,
                rows :res.obj.list
            }
        },
        columns: [
            {
                title: '序号',
                field: 'id',
                align: 'center',
                valign: 'middle',
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {
                title: '班级',
                field: 'ban',
                align: 'center',
                valign: 'middle',
                formatter:function (id,row) {
                    var gradeName = row.gradeName;
                    var className = row.className;
                    return gradeName+""+className;
                }
            },
            {
                title: '教师名称',
                align: 'center',
                field: 'teacherName',
                valign: 'middle',

            } ,
            {
                title: '星期',
                align: 'center',
                field: 'weekNumVal',
                valign: 'middle',

            }  ,
            {
                title: '时间',
                align: 'center',
                field: 'courseTime',
                valign: 'middle',

            }  ,
            {
                title: '操作',
                align: 'center',
                field: 'id',
                width:240,
                valign: 'middle',
                formatter:function (id,row) {
                    return '<a onclick="removeTr(this)" data-id="'+id+'" class="btn btn-danger" href="javascript:;">删除</a> &nbsp<a data-id="'+id+'" onclick="update(this)" class="btn btn-primary " href="javascript:;">修改</a>';
                }
            }
        ]
    });
}



function removeTr(obj){
   tan("是否确认删除",function (index,layero) {
       var id = $(obj).data("id");
       $.DemoGet("/courseMould/delete/"+id,function (data) {
           if(data.success)
           {
               layer.close(index);
               $("#paike_list").bootstrapTable('refresh');
           }
           else {
               layer.msg(data.msg);
           }
       },function (e) {
           layer.msg("删除数据失败");
       });
   });
}

function add(){
    $("#classList").val("");
    $("#time").val("");
    $("#weekNum").val("");
    popdiv("class-info","添加",["50%","50%"],function (index,layero) {
        var classId = $("#classList").val();
        var courseTime = $("#time").val();
        var weekNum = $("#weekNum").val();
        if(classId=='')
        {
            layer.msg("请选择班级");
            return;
        }
       else if(courseTime=='')
        {
            layer.msg("请选择上课时间");
            return;
        }
        else if(weekNum=='')
        {
            layer.msg("请选择日期");
            return;
        }
        else {
            $.DemoPost("/courseMould/addByUserInfo?token="+sessionStorage.getItem("token"),
                {"classId":classId,"courseTime":courseTime,"weekNum":weekNum},
                function (data) {
                    if(data.success)
                    {
                        layer.close(index);
                        $("#paike_list").bootstrapTable('refresh');
                    }
                    else {
                        layer.msg(data.msg);
                    }
                },function (e) {
                    layer.msg("添加数据失败");
                });
        }
    })
}
function update(obj){
    var id = $(obj).data("id");
    $.DemoGet("/courseMould/info/"+id,function (data) {
        if(data.success)
        {
            $("#classList").val(data.obj.classId);
            $("#time").val(data.obj.courseTime);
            $("#weekNum").val(data.obj.weekNum);
            $("#id").val(data.obj.id);
            popdiv("class-info","修改",["50%","50%"],function (index,layero) {
                var classId = $("#classList").val();
                var courseTime = $("#time").val();
                var weekNum = $("#weekNum").val();
                var id = $("#id").val();
                if(classId=='')
                {
                    layer.msg("请选择班级");
                    return;
                }
                else if(courseTime=='')
                {
                    layer.msg("请选择上课时间");
                    return;
                }
                else if(weekNum=='')
                {
                    layer.msg("请选择日期");
                    return;
                }
                else {
                    $.DemoPost("/courseMould/update",
                        {"classId":classId,"courseTime":courseTime,"weekNum":weekNum,"id":id},
                        function (data) {
                            if(data.success)
                            {
                                layer.close(index);
                                $("#paike_list").bootstrapTable('refresh');
                            }
                            else {
                                layer.msg(data.msg);
                            }
                        },function (e) {
                            layer.msg("修改数据失败");
                        });
                }
            })
        }
        else {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("获取详情信息失败");
    });

}
//时分秒
getTime("time","time");



$("#btnQuery").click(function () {
    $("#paike_list").bootstrapTable('refresh');
});